//
//  YXLoginViewController.m
//  CaoShi
//
//  Created by aa on 2018/4/25.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXLoginViewController.h"
#import "YXRegistViewController.h"
#import "YXLoginViewModel.h"
@interface YXLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (nonatomic ,strong) YXLoginViewModel *loginVM;
@end

@implementation YXLoginViewController

- (YXLoginViewModel *)loginVM {
    if (!_loginVM){
        _loginVM = [[YXLoginViewModel alloc] init];
    }
    return _loginVM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    
    [self setNar];
    [self setBindUI];
    
}

- (void)setNar {
    WZHNavBarItem *leftItem = [WZHNavBarItem itemWithImageName:@"left_hei" target:self action:@selector(leftItemClick)];
    self.w_navLeftBarButtonItem = leftItem;
    
    WZHNavBarItem *item = [WZHNavBarItem itemWithTitle:@"注册" target:self action:@selector(itemClick)];
    item.textColor = RGB(248, 110, 14);
    self.w_navRightBarButtonItem = item;
    
}


- (void)setBindUI {
    // 绑定控件
    RAC(self.loginVM,account) = _phoneTF.rac_textSignal;
    RAC(self.loginVM,pwd) = _pwdTF.rac_textSignal;
    
    RAC(self.loginBtn,enabled) = self.loginVM.loginEnableSignal;
    
    
    [self.loginVM.loginCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
        NSLog(@"登录成功，跳转页面");
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];

    }];
    
    [[_loginBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"点击了  点击了");
        [self.loginVM.loginCommand execute:@{@"account":_phoneTF.text,@"password":_pwdTF.text}];
    }];
    
}



- (void)leftItemClick {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)itemClick {
    
    YXRegistViewController *vc = [[YXRegistViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}



@end
